package com.magold.logic;

import org.andengine.entity.scene.Scene;
import org.andengine.entity.shape.IAreaShape;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.magold.circuit.ICircuit;
import com.magold.circuit.ICircuitPath;
import com.magold.circuit.module.CircuitModule;
import com.magold.context.IContext;
import com.magold.logic.region.LightingRegion;
import com.magold.physics.MagoldPhysicsConnector;
import com.magold.sprite.IMultiTextureSprite;
import com.magold.sprite.MultiTextureSprite;
/**
 * 
 * @author cqq
 *
 */
public class SolarCell implements ISpriteLogic, CircuitModule {
	
	public static abstract class Constants extends LogicConstants {
		protected ITiledTextureRegion mTexture;
		protected ITiledTextureRegion mSolarTexture;
		protected FixtureDef mFixtureDef;
		
		public Constants(IContext pActivity) {
			super(pActivity);
		}
	}
	
	private static Constants sConstants;
	public static void customize(Constants pConstants) {
		sConstants = pConstants;
	}
	private IMultiTextureSprite mSprite;
	private MagoldPhysicsConnector mConnector;
	
	private final boolean mIsSolar;
	private boolean mIsConnected;
	private int mOnLightRegionCount;
	
	public SolarCell(boolean pIsSolar) {
		mIsSolar = pIsSolar;
		mIsConnected = !pIsSolar;
	}
	
	@Override
	public MagoldPhysicsConnector findPhysicsConnector(IAreaShape pShape) {
		if(pShape == mSprite) return mConnector;
		
		return null;
	}

	@Override
	public void createSceneData(float pX, float pY, float pRotation,
			float pScale, Scene pScene) {
		VertexBufferObjectManager vbom = sConstants.mContext.getVertexBufferObjectManager();
		
		ITiledTextureRegion texture = sConstants.mSolarTexture;
		MultiTextureSprite solar = new MultiTextureSprite(pX, pY, 0, texture, vbom);
		solar.setZIndex(1);
		pScene.attachChild(solar);
		
		MultiTextureSprite rawSprite = new MultiTextureSprite(pX, pY, 0, sConstants.mTexture, vbom);
		mSprite = LogicSpriteWrapper.wrap(rawSprite, pScale, sConstants, this, pScene);
		pScene.sortChildren();
	}

	@Override
	public void createPhysicsData(PhysicsWorld pWorld) {
		Body body = PhysicsFactory.createBoxBody(pWorld, mSprite, BodyType.StaticBody, sConstants.mFixtureDef);
		
		mConnector = LogicPhysicsHelper.connectBody(pWorld, body, mSprite);
	}

	@Override
	public boolean isStatic() {
		return true;
	}

	@Override
	public void startCollide(ISpriteLogic pAnthor, Contact pContact) {
		if((pAnthor instanceof LightingRegion) && mIsSolar) {
			attachLightRegion();
		}
	}

	@Override
	public void endCollide(ISpriteLogic pAnthor, Contact pContact) {
		if((pAnthor instanceof LightingRegion) && mIsSolar) {
			detachLightRegion();
		}
	}
	
	private void attachLightRegion() {
		mIsConnected = true;
		mOnLightRegionCount++;
	}
	
	private void detachLightRegion() {
		if(--mOnLightRegionCount == 0) {
			mIsConnected = false;
		}
		
	}

	@Override
	public boolean isConnected() {
		return mIsConnected;
	}

	@Override
	public void onCurrentBegin() {
	}

	@Override
	public void onCurrentEnd() {
	}

	@Override
	public void onPathJoined(ICircuit pCircuit, ICircuitPath pPath) {
	}

	private int mID;
	@Override
	public void setID(int pID) {
		mID = pID;
	}

	@Override
	public int getID() {
		return mID;
	}

	@Override
	public float[] getWireInPosition() {
		return mSprite.convertLocalToSceneCoordinates(16, 65);
	}

	@Override
	public float[] getWireOutPosition() {
		return mSprite.convertLocalToSceneCoordinates(16, 0);
	}

}
